package com.feicheng.controller.wx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.DateUtils;
import com.feicheng.persistence.entity.Askjl;
import com.feicheng.persistence.service.IAfjService;
import com.feicheng.persistence.service.IAmdService;
import com.feicheng.persistence.service.IAskjlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.feicheng.service.impl.FMjConnectionService;

import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/wx/skjl")
public class SkjlController {

    @Autowired
    IAfjService iAfjService;

    @Autowired
    IAmdService iAmdService;

    @Autowired
    IAskjlService iAskjlService;

    @Autowired
    FMjConnectionService fMjConnectionService;

    @PostMapping("/date/remove")
    public ResultInfo removeSkjl(@RequestBody JSONObject param) {
        String skjlid = param.getString("skjlid");
        Askjl skjl = iAskjlService.getById(skjlid);
        iAskjlService.removeById(skjlid);
        fMjConnectionService.removeFaceToMjBySkjl(skjl);
        return ResultInfo.success(param);
    }

    @PostMapping("/date/post")
    public ResultInfo newSkjlByDate(@RequestBody JSONObject param) {

        Askjl skjl = param.getObject("skjl", Askjl.class);

        iAskjlService.save(skjl);

        LocalDate now = LocalDate.now();
        if (now.isEqual(skjl.getRq())) {
            fMjConnectionService.addFaceToMjBySkjl(skjl);
        }

        return ResultInfo.success(param);
    }

    @Transactional
    @PostMapping("/date/all/post")
    public ResultInfo newSkjlsByDate(@RequestBody JSONObject param) {

        String mdid = param.getString("mdid");
        String fjid = param.getString("fjid");
        String sd = param.getString("sd");
        String ed = param.getString("ed");
        JSONObject dateObj = param.getJSONObject("dateObj");

        QueryWrapper<Askjl> skjlqw = new QueryWrapper<>();
        skjlqw.eq("mdid", mdid);
        skjlqw.eq("fjid", fjid);
        skjlqw.ge("sd", sd);
        skjlqw.le("ed", ed);
        iAskjlService.remove(skjlqw);

        List<Askjl> askjlList = new ArrayList<>();
        for (Iterator<String> it = dateObj.keySet().iterator(); it.hasNext();) {
            String key = it.next();
            JSONObject rt = dateObj.getJSONObject(key);
            List<Askjl> skjls = rt.getObject("skjls", new TypeReference<List<Askjl>>() {});
            if (null != skjls && skjls.size() > 0) {
                skjls.forEach(skjl -> {
                    askjlList.add(skjl);
                });
            }
        }

        iAskjlService.saveBatch(askjlList);

        fMjConnectionService.addFaceToMjBySkjls(askjlList);

        return ResultInfo.success(param);
    }

    @PostMapping("/date/get")
    public ResultInfo getSkjlByDate(@RequestBody JSONObject param) {

        QueryWrapper<Askjl> qw = new QueryWrapper<>();
        String mdid = param.getString("mdid");
        if (StringUtils.hasLength(mdid)) {
            qw.eq("mdid", mdid);
        }

        String fjid = param.getString("fjid");
        if (StringUtils.hasLength(fjid)) {
            qw.eq("fjid", fjid);
        }

        String sd = param.getString("sd");
        if (StringUtils.hasLength(sd)) {
            qw.ge("rq", sd);
        }

        String ed = param.getString("ed");
        if (StringUtils.hasLength(ed)) {
            qw.le("rq", ed);
        }

        String tel = param.getString("tel");
        if (StringUtils.hasLength(tel)) {
            qw.eq("tel", tel);
        }

        Map<String, JSONObject> dateObj = new TreeMap<String, JSONObject>();
        LocalDate lsd = DateUtils.stringToDate("yyyy-MM-dd", sd);
        LocalDate led = DateUtils.stringToDate("yyyy-MM-dd", ed);
        for (LocalDate ctd = lsd.plusDays(0L); !ctd.isAfter(led); ctd = ctd.plusDays(1L)) {
            JSONObject rt = new JSONObject();
            rt.put("index", dateObj.size());
            rt.put("week", ctd.getDayOfWeek().getValue());
            rt.put("weekStr", DateUtils.getWeekStr(ctd.getDayOfWeek().getValue()));
            rt.put("skjls", new JSONArray());
            dateObj.put(DateUtils.dateToString("yyyy-MM-dd", ctd), rt);
        }

        List<Askjl> askjlList = iAskjlService.list(qw);
        if (null != askjlList && askjlList.size() > 0) {
            askjlList.forEach(skjl -> {
                String rqkey = DateUtils.dateToString("yyyy-MM-dd", skjl.getRq());
                JSONObject rt = dateObj.get(rqkey);
                JSONArray skjls = rt.getJSONArray("skjls");
                skjls.add(skjl);
            });
        }

        param.put("dateObj", dateObj);
        return ResultInfo.success(param);
    }


    /**
     * 房间分页查询
     * @param param
     * @return
     */
    @PostMapping("/page/get")
    public ResultInfo getSkjlByPage(@RequestBody JSONObject param) {

        Integer current = param.getInteger("current");
        Integer size = param.getInteger("size");

        if (current == null || current < 0) {
            current = 1;
        }

        if (size == null || size < 0) {
            size = 16;
        }

        // 构建分页
        Page<JSONObject> page = new Page<JSONObject>(current, size);
        QueryWrapper<JSONObject> qwfj = new QueryWrapper<>();

        String fjid = param.getString("fjid");
        if (StringUtils.hasLength(fjid)) {
            qwfj.eq("fjid", fjid);
        }

        return ResultInfo.success(param);

    }

    /**
     * 房间详细查询
     * @param param
     * @return
     */
    @PostMapping("/info/get")
    public ResultInfo getFjInfo(@RequestBody JSONObject param) {


        return ResultInfo.success(param);
    }

}
